package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.TwoMapper;
import com.example.demo.pojo.Two;
import com.example.demo.pojo.TwoDto;
import com.example.demo.service.TwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Classname TwoServiceImpl
 * @Description TODO
 * @Date 2023/5/20 9:57
 * @Created by Zh_Cn
 */
@Service
public class TwoServiceImpl extends ServiceImpl<TwoMapper, Two> implements TwoService {
    @Autowired
    TwoMapper twoMapper;
    @Override
    public Map<String, Integer> getprices() {
        List<Two> twos = twoMapper.selectList(null);
        Map<String, Integer> priceRangeMap = new LinkedHashMap<>();

        // 初始化价格区间及其对应的统计数
        priceRangeMap.put("0-20", 0);
        priceRangeMap.put("20-40", 0);
        priceRangeMap.put("40-80", 0);
        priceRangeMap.put("80-120", 0);
        priceRangeMap.put("120以上", 0);

        // 遍历所有的书籍，根据价格区间进行统计
        for (Two sBook : twos) {
            String priceString = sBook.getPrice();
            String numericPart = priceString.substring(2);
            float price = Float.parseFloat(numericPart);

            if (price < 20) {
                priceRangeMap.put("0-20", priceRangeMap.get("0-20") + 1);
            } else if (price < 40) {
                priceRangeMap.put("20-40", priceRangeMap.get("20-40") + 1);
            } else if (price < 80) {
                priceRangeMap.put("40-80", priceRangeMap.get("40-80") + 1);
            } else if (price < 120) {
                priceRangeMap.put("80-120", priceRangeMap.get("80-120") + 1);
            } else {
                priceRangeMap.put("120以上", priceRangeMap.get("120以上") + 1);
            }
        }
        return priceRangeMap;
    }

    @Override
    public List<TwoDto> getrainbow(Integer type) {
        List<Two> twoList = twoMapper.selectList(new LambdaQueryWrapper<Two>()
                .select(Two::getSales,Two::getPrice)
                .eq(Two::getType, changeName(type)));
        List<TwoDto> twoDtos = twoList.stream().map(item -> {
            TwoDto twoDto = new TwoDto();
            String trim = item.getPrice().replaceAll("￥ ", "").trim();
            twoDto.setPrice(Float.parseFloat(trim));
            twoDto.setSales(item.getSales());
            return twoDto;
        }).collect(Collectors.toList());
        return twoDtos;
    }

    private String changeName(Integer type){
        String nameType=null;
        switch (type){
            case 1:
                nameType="人文艺术";
                break;
            case 2:
                nameType="人文社科";
                break;
            case 3:
                nameType="少儿童书";
                break;
            case 4:
                nameType="教育考试";
                break;
            case 5:
                nameType="经济金融";
                break;
            default:
                nameType="生活休闲";
        }
        return nameType;
    }
    @Override
    public Map<String, Integer> getTypeAndPrice(Long start, Long end) {
        // 从数据库中取出数据
        List<Two> twos = twoMapper.selectList(null);

        // 统计Type类型为XXX并且价格为start--end的数据数量
        Map<String, Integer> typeAndCountMap = new HashMap<>();
        for (Two sBook : twos) {
            String priceString = sBook.getPrice();
            String numericPart = priceString.substring(2);
            float price = Float.parseFloat(numericPart);

            // 判断价格是否在指定区间内
            if (price >= start && price <= end) {
                String bookType = sBook.getType();

                // 如果Map中已存在该类型，则数量加1；否则，将该类型添加到Map中，并设置初始数量为1
                if (typeAndCountMap.containsKey(bookType)) {
                    int count = typeAndCountMap.get(bookType);
                    typeAndCountMap.put(bookType, count + 1);
                } else {
                    typeAndCountMap.put(bookType, 1);
                }
            }
        }
        // 返回结果Map
        return typeAndCountMap;
    }

    /**
     * 谢宇说List里面存map不好解析 只有换Dto
    @Override
    public List<Map<String,Object>> getrainbow() {
        //从数据库中取出数据
        List<Two> twos = twoMapper.selectList(null);
        //创建一个数组存储数据
        List<Map<String, Object>> resultList = new ArrayList<>();
        //对取出数据的金额进行float处理
        for (Two sBook : twos) {
            String priceString = sBook.getPrice();
            String numericPart = priceString.substring(2);
            float price = Float.parseFloat(numericPart);
            //创建一个map存储价格 销量 书籍类型
            Map<String,Object> data = new HashMap<>();
            data.put("价格",price);
            data.put("销量",sBook.getSales());
            data.put("书籍类型",sBook.getType());
            // 将Map添加到结果列表中
            resultList.add(data);
        }
        //返回
        return resultList;
    }
    **/


}
